<div class="layui-card">
  <form class="layui-form" lay-filter="add-goods-form">
		<ul class="typeTitle">
			<li class="li1">商品</li>
			<li class="li2">预估回收价</li>
			<li class="li3">审核操作</li>
			<li class="li4">审核结果</li>
		</ul>
    <ul class="money" id="money">
			<!-- <li class="ppx1" data-id="'+data.data.books[i].book_id+'">
				<img src="'+data.data.books[i].book_image+'"/>
				<div class="title">
					<p class="p1">'+data.data.books[i].book_title+'</p>
					<p>'+data.data.books[i].book_author+'</p>
					<p>'+data.data.books[i].book_isbn+'</p>
				</div>
				<div class="price">
					<p>预估回收价：￥ <span class="price_1">'+data.data.books[i].book_recyle_price+'</span></p>
				</div>
				<div class="pass">
					<button class="layui-btn layui-btn-xs" lay-submit lay-filter="passInfo" id="passInfo">验收通过</button><br><br>
					<button class="layui-btn layui-btn-danger layui-btn-xs" lay-submit lay-filter="rejection" id="rejection">拒收</button><br><br>
					<button class="layui-btn layui-btn-warm layui-btn-xs" lay-submit lay-filter="noPass" id="noPass">没收到这本书</button><br>
				</div>
				<div class="pass1">
					<p>全新书</p>
				</div>
			</li> -->
    </ul>
   <div class="msg">
   	   <div style="width: 100%;overflow: auto;">
   	   	<p style="float: right;border-bottom: 1px solid #ccc;width: 15%;text-align: right;">共 <span id="books_total"></span>本书，<span id="rejection">0</span>本被拒收</p>

   	   </div>
   	   <p style="text-align: right;">总估价：¥ <span id="total_price"></span></p>
   	   <p style="text-align: right;">品相中等（<span id="mid">0</span>本）：-¥ <span id="mid_price">0</span></p>
   	   <p style="text-align: right;">未通过（ <span id="rejection1">0</span>本）：-¥ <span id="rejection_price">0</span></p>
   	   <p style="text-align: right;">最终收入：¥ <span id="resultBooks">0</span></p>
   </div>
    <div class="layui-form-item" style="text-align: center;padding-bottom: 30px;" >
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo" id="formDemo">确认审核结果并打款</button>
        </div>
    </div>
  </form>

<script>
    //Demo
  layui.use(['admin','form'], function(){
    var form = layui.form;
		var $ = layui.$;
		var admin = layui.admin;
		var view = layui.view;
    var sell_order_id = layui.router().search.sell_order_id;
    // 获取信息
    $.ajax({
		    type: "GET",
		    dataType: "json",
		    url: 'https://bookshop.dingdangtoy.com/api/admin/sell/order/show?sell_order_id=' + sell_order_id,
		    data: {},
		    headers: { //通过 request 头传递
	          'access-token': layui.data('layuiAdmin').access_token
	        },
			success: function(data){
				var _html = '';
				$('#books_total').text(data.data.books_total);
				$('#total_price').text(data.data.total_price);
				for(var i=0;i<data.data.books.length;i++){
					if(parseInt(data.data.books[i].first_check_state) == 0){
						_html += '<li class="ppx1" data-id="'+data.data.books[i].book_id+'">'+
							    		'<img src="'+data.data.books[i].book_image+'"/>'+
							    		'<div class="title">'+
								    		'<p class="p1">'+data.data.books[i].book_title+'</p>'+
								    		'<p>作者：'+data.data.books[i].book_author+'</p>'+
								    		'<p>ISBN：'+data.data.books[i].book_isbn+'</p>'+
								    		'<p>原价：￥'+data.data.books[i].book_old_price+'</p>'+
							    		'</div>'+
							    		'<div class="price">'+
							    			'<p>￥ <span class="price_1">'+data.data.books[i].book_recyle_price+'</span></p>'+
							    		'</div>'+
											'<div class="pass" style="line-height:110px">/</div>'+
											'<div class="pass1">'+
												'<p class="resultInfo" data-recyle="'+data.data.books[i].book_recyle_price+'" data-type="" data-state="2" style="color:red" data-id="'+data.data.books[i].book_id+'">线上审核未通过</p>'+
											'</div>'+
							    	'</li>'
					}else{
						_html += '<li class="ppx1" data-id="'+data.data.books[i].book_id+'">'+
							    		'<img src="'+data.data.books[i].book_image+'"/>'+
							    		'<div class="title">'+
								    		'<p class="p1">'+data.data.books[i].book_title+'</p>'+
								    		'<p>作者：'+data.data.books[i].book_author+'</p>'+
								    		'<p>ISBN：'+data.data.books[i].book_isbn+'</p>'+
								    		'<p>原价：￥'+data.data.books[i].book_old_price+'</p>'+
							    		'</div>'+
							    		'<div class="price">'+
							    			'<p>￥ <span class="price_1">'+data.data.books[i].book_recyle_price+'</span></p>'+
							    		'</div>'+
											'<div class="pass">'+
												'<button class="layui-btn layui-btn-xs passInfo" lay-submit lay-filter="passInfo">验收通过</button><br><br>'+
												'<button class="layui-btn layui-btn-danger layui-btn-xs rejection" lay-submit lay-filter="rejection">拒收</button><br><br>'+
												'<button class="layui-btn layui-btn-primary layui-btn-xs noPass" lay-submit lay-filter="noPass">没收到这本书</button><br>'+
											'</div>'+
											'<div class="pass1" style="line-height: none;">'+
												'<p class="resultInfo" data-recyle="'+data.data.books[i].book_recyle_price+'" data-type="" data-state="" data-id="'+data.data.books[i].book_id+'"></p>'+
												'<p class="recyleRrice" style="color:green;margin-top:10px;display:none"></p>'+
											'</div>'+
							    	'</li>'
					}
				}
	      $("#money").append(_html);
				form.render();
			},
			error: function(msg){
			  console.log(msg)
			}
		});
		// 求和
		function sum(arr) {
			return eval(arr.join("+"));
		};
		// 四舍五入
		function toDecimal(num, n) {
			n = n ? parseInt(n) : 0;
			if(n <= 0) {
				return Math.round(num);
			}
			num = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); //四舍五入
			num = Number(num).toFixed(n); //补足位数
			return num;
		}
		// 点击通过弹窗
		$(document).on('click', '#money li .passInfo', function(){
			var contentText = $(this).parents('.ppx1').find('.resultInfo');
			var recyleRrice = $(this).parents('.ppx1').find('.recyleRrice');
			admin.popup({
				title: '验收通过',
				area: ['400px', '500px'],
				id: 'LAY-popup-content-edit',
				success: function(layero, index) {
					view(this.id).render('buy-orders/not-pay/passInfo').done(function() {
						form.render(null, 'layuiadmin-form-tags1');
						//监听提交
						form.on('submit(formGoods)', function(data){
							contentText.text($('#tips li').find("input[type='radio']:checked").attr('title'))
							contentText.attr('data-type',$('#tips li').find("input[type='radio']:checked").val())
							contentText.attr('data-state',1);
							contentText.css('color','green');
							var booksType = $('#tips li').find("input[type='radio']:checked").val();
							var booksPirces = contentText.attr('data-recyle');
							var mids = '';
							console.log('类型：'+booksType)
							console.log('价格：'+booksPirces)
							if(parseInt(booksType)===1||parseInt(booksType)===2){
								recyleRrice.text('￥'+booksPirces);
								recyleRrice.show();
							}else{
                                mids=toDecimal(parseFloat(booksPirces)*0.75,1)
								recyleRrice.text('￥'+mids)
								recyleRrice.show();
							}
							var dataArray = [];
							// 未通过本书
							var arrNum1 = [];
							// 品相中等本书
							var arrNum2 = [];
							$('#money').children('li').each(function(){
							var self_reason = $(this).find('.resultInfo').text();
							var self_type = $(this).find('.resultInfo').attr('data-type');
							var self_book_id = $(this).find('.resultInfo').attr('data-id');
							var self_state = $(this).find('.resultInfo').attr('data-state');
							var self_recyle = $(this).find('.resultInfo').attr('data-recyle');
							var data = {
							'reason': self_reason,
							'type': self_type,
							'state': self_state,
							'book_id': self_book_id,
							'book_recyle_price':self_recyle
							};
							dataArray.push(data);
							});
							dataArray.forEach(function(val){
								if (parseInt(val.state) == 2 || parseInt(val.state) == 3) {
									arrNum1.push(val.book_recyle_price)
								} else if (parseInt(val.type) == 3 ||parseInt(val.type) == 4 ||parseInt(val.type) == 5 ||parseInt(val.type) == 6 ||parseInt(val.type) == 7 ||parseInt(val.type) == 8) {
									arrNum2.push(val.book_recyle_price)
								}
							})
							$('#rejection').text(arrNum1.length)
							$('#rejection1').text(arrNum1.length)
							arrNum2.forEach(function(val,index){
								arrNum2[index] = toDecimal(parseFloat(val)*0.25,1)
							})
							var omg1 = toDecimal(sum(arrNum1),1);
							var omg2 = toDecimal(sum(arrNum2),1);
							if(omg1 == 'NaN'){
								omg1 = 0;
							}
							if(omg2 == 'NaN'){
								omg2 = 0;
							}
							$('#rejection_price').text(omg1)
							$('#mid').text(arrNum2.length)
							$('#mid_price').text(omg2)
							var fng1 = parseFloat($('#total_price').text())
							var fng2 = parseFloat($('#mid_price').text())
							var fng3 = parseFloat($('#rejection_price').text())
							if(fng2){
								fng2 = parseFloat($('#mid_price').text())
							}else{
                                fng2 = 0
							}
							if(fng3){
								fng3 = parseFloat($('#rejection_price').text())
							}else{
                                fng3 = 0
							}
							var fng4 = toDecimal(fng1-fng2-fng3,1)
							$('#resultBooks').text(fng4)
							layer.close(index);
							return false;
						})
					});
				}
			});
			return false;
		});
		// 点击拒收弹窗
		$(document).on('click', '#money li .rejection', function(){
			var contentText = $(this).parents('.ppx1').find('.resultInfo');
			var recyleRrice = $(this).parents('.ppx1').find('.recyleRrice');
			admin.popup({
				title: '拒收',
				area: ['400px', '250px'],
				id: 'LAY-popup-content-edit1',
				success: function(layero, index) {
					view(this.id).render('buy-orders/not-pay/rejection').done(function() {
						form.render(null, 'layuiadmin-form-tags2');
						//监听提交
						form.on('submit(formGoods1)', function(data){
							recyleRrice.hide();
							contentText.text('拒收原因：' + $('#tips1').val())
							contentText.attr('data-state',2)
							contentText.css('color','red')
							var dataArray = [];
							// 未通过本书
							var arrNum1 = [];
							// 品相中等本书
							var arrNum2 = [];
							$('#money').children('li').each(function(){
							var self_reason = $(this).find('.resultInfo').text();
							var self_type = $(this).find('.resultInfo').attr('data-type');
							var self_book_id = $(this).find('.resultInfo').attr('data-id');
							var self_state = $(this).find('.resultInfo').attr('data-state');
							var self_recyle = $(this).find('.resultInfo').attr('data-recyle');
							var data = {
							'reason': self_reason,
							'type': self_type,
							'state': self_state,
							'book_id': self_book_id,
							'book_recyle_price':self_recyle
							};
							dataArray.push(data);
							});
							dataArray.forEach(function(val){
                if (parseInt(val.state) == 2 || parseInt(val.state) == 3) {
								  arrNum1.push(val.book_recyle_price)
							  } else if (parseInt(val.type) == 3 ||parseInt(val.type) == 4 ||parseInt(val.type) == 5 ||parseInt(val.type) == 6 ||parseInt(val.type) == 7 ||parseInt(val.type) == 8) {
                  arrNum2.push(val.book_recyle_price)
							  }
							})
							$('#rejection').text(arrNum1.length)
							$('#rejection1').text(arrNum1.length)
							$('#rejection_price').text(toDecimal(sum(arrNum1),1))
							arrNum2.forEach(function(val,index){
								arrNum2[index] = toDecimal(parseFloat(val)*0.25,1)
							})
							var omg1 = toDecimal(sum(arrNum1),1);
							var omg2 = toDecimal(sum(arrNum2),1);
							if(omg1 == 'NaN'){
								omg1 = 0;
							}
							if(omg2 == 'NaN'){
								omg2 = 0;
							}
							$('#rejection_price').text(omg1)
							$('#mid').text(arrNum2.length)
							$('#mid_price').text(omg2)
							var fng1 = parseFloat($('#total_price').text())
							var fng2 = parseFloat($('#mid_price').text())
							var fng3 = parseFloat($('#rejection_price').text())
							if(fng2){
								fng2 = parseFloat($('#mid_price').text())
							}else{
                fng2 = 0
							}
							if(fng3){
								fng3 = parseFloat($('#rejection_price').text())
							}else{
                fng3 = 0
							}
							var fng4 = toDecimal(fng1-fng2-fng3,1)
							$('#resultBooks').text(fng4)
							layer.close(index);
							return false;
						})
					});
				}
			});
			return false;
		});
		// 点击确认没有收到书
		$(document).on('click', '#money li .noPass', function(){
			var contentText = $(this).parents('.ppx1').find('.resultInfo');
			var recyleRrice = $(this).parents('.ppx1').find('.recyleRrice');
			layer.confirm('是否确认没有收到这本书？', function(index){
				recyleRrice.hide();
				contentText.text('未收到这本书')
				contentText.attr('data-state',3)
				contentText.css('color','red')
				var dataArray = [];
				// 未通过本书
				var arrNum1 = [];
				// 品相中等本书
				var arrNum2 = [];
				$('#money').children('li').each(function(){
				var self_reason = $(this).find('.resultInfo').text();
				var self_type = $(this).find('.resultInfo').attr('data-type');
				var self_book_id = $(this).find('.resultInfo').attr('data-id');
				var self_state = $(this).find('.resultInfo').attr('data-state');
				var self_recyle = $(this).find('.resultInfo').attr('data-recyle');
				var data = {
				'reason': self_reason,
				'type': self_type,
				'state': self_state,
				'book_id': self_book_id,
				'book_recyle_price':self_recyle
				};
				dataArray.push(data);
				});
				dataArray.forEach(function(val){
					if (parseInt(val.state) == 2 || parseInt(val.state) == 3) {
					arrNum1.push(val.book_recyle_price)
					} else if (parseInt(val.type) == 3 ||parseInt(val.type) == 4 ||parseInt(val.type) == 5 ||parseInt(val.type) == 6 ||parseInt(val.type) == 7 ||parseInt(val.type) == 8) {
					arrNum2.push(val.book_recyle_price)
					}
				})
				$('#rejection').text(arrNum1.length)
				$('#rejection1').text(arrNum1.length)
				$('#rejection_price').text(toDecimal(sum(arrNum1),1))
				arrNum2.forEach(function(val,index){
					arrNum2[index] = toDecimal(parseFloat(val)*0.25,1)
				})
				var omg1 = toDecimal(sum(arrNum1),1);
				var omg2 = toDecimal(sum(arrNum2),1);
				if(omg1 == 'NaN'){
					omg1 = 0;
				}
				if(omg2 == 'NaN'){
					omg2 = 0;
				}
				$('#rejection_price').text(omg1)
				$('#mid').text(arrNum2.length)
				$('#mid_price').text(omg2)
				var fng1 = parseFloat($('#total_price').text())
				var fng2 = parseFloat($('#mid_price').text())
				var fng3 = parseFloat($('#rejection_price').text())
				if(fng2){
					fng2 = parseFloat($('#mid_price').text())
				}else{
					fng2 = 0
				}
				if(fng3){
					fng3 = parseFloat($('#rejection_price').text())
				}else{
					fng3 = 0
				}
				var fng4 = toDecimal(fng1-fng2-fng3,1)
				$('#resultBooks').text(fng4)
				layer.close(index);
			});
			return false;
		});

		// 切换单选状态
		$(document).on('click', '#money li', function(){
      var result = $(this).find("input[type='radio']:checked").val();
      if (parseInt(result) === 1) {
      	$(this).find('.type1').show();
      	$(this).find('.info-msg1').text('');
      	$(this).find('.reason').val('');
      	$(this).find('.type2').hide();
      }else{
      	$(this).find('.type2').show();
      	$(this).find('.type1').hide();
//    	$(this).find(".type1 input[type='radio']:checked").prop('checked', false);
//    	$('input:radio[name="likea"]').removeAttr('checked');
      	$(this).find(".type1 input[type='radio']").removeAttr('checked');
      	form.render('radio')
      	$(this).find('.reason').val('');
      	$(this).find('.info-msg1').text('');
      }
		});
    $(document).on('click', '.layui-btn-1', function(){
    	var info_msgg = $(this).parents('.type1').find("input[type='radio']:checked").val();
    	console.log($(this).parents('.pass').find('info-msg1'))
    	$(this).parents('.pass').find('.info-msg1').text('审核通过：'+info_msgg);
    	$(this).parents('.pass').find('.info-msg1').css('color','green')
    	return false;
    })
    // 拒收
    $(document).on('click', '.layui-btn-2', function(){
    	var info_msgg1 = $(this).parents('.type2').find('.reason').val();
      console.log($(this).parents('.type2').find('.reason').val())
    	$(this).parents('.pass').find('.info-msg1').text('拒收：'+info_msgg1);
    	$(this).parents('.pass').find('.info-msg1').css('color','red')
    	return false;
    })
    function toDecimal(num, n) {
	    n = n ? parseInt(n) : 0;
	    if(n <= 0) {
	        return Math.round(num);
	    }
	    num = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); //四舍五入
	    num = Number(num).toFixed(n); //补足位数
	    return num;
		}
    form.render(null, 'add-goods-form')

    //监听提交
	  form.on('submit(formDemo)', function(data){
	    var arr1 = [];
        $('#money').children('li').each(function(){
			var self_reason = $(this).find('.resultInfo').text();
			var self_type = $(this).find('.resultInfo').attr('data-type');
			var self_book_id = $(this).find('.resultInfo').attr('data-id');
			var self_state = $(this).find('.resultInfo').attr('data-state');
			var data = {
			'reason': self_reason,
			'type': self_type,
			'state': self_state,
			'book_id': self_book_id
			};
			arr1.push(data);
		});
		arr1 = JSON.stringify(arr1)
		console.log(arr1)
	    layer.confirm('提交后，审核的最终收入将直接转入用户余额账户中，是否确认操作？', function(index){
   	    $.ajax({
			    type: "POST",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/sell/order/check",
			    data: {
			    	'sell_order_id':sell_order_id,
			    	'books':arr1,
			    },
			    headers: {
	              'access-token': layui.data('layuiAdmin').access_token
	            },
				success: function(data){
				  if(data.code == 0){
                    layer.msg('操作成功');
				  	location.hash= '/buy-orders/payed/'
				  } else {
				  	layer.msg(data.message);
				  }

				},
				error: function(msg){
				  console.log(msg)
				}
			});
        layer.close(index);
      });

	    return false;
	  });
  });
</script>
<style>
	.msg{
		width: 96%;
		margin: 0 auto;
		overflow: auto;
	}
    .money{
    	padding: 10px;
    	overflow: auto;
    }
    .money li{
    	width: 100%;
    	height: 110px;
    	padding-bottom: 10px;
    	margin-bottom: 10px;
    	clear: both;
    	border-bottom: 1px solid #ccc;
    }
    .money li img{
    	width: 82px;
    	height: 110px;
    	display: block;
    	float: left;
    	margin-right: 20px;
    }
    .money li .title{
    	width: 300px;
    	float: left;
    }
    .money li .title .p1{
    	margin-bottom: 10px;
    }
    .money li .price {
    	float: left;
    	width: 180px;
    	height: 110px;
      line-height: 100px;
	  text-align: center;
    }
    .money li .pass{
    	float: left;
    	width: 180px;
		height: 110px;
		text-align: center;
	}
	.money li .pass1{
	  float: left;
		width: 240px;
		height: 110px;
		overflow: hidden;
		text-align: center;
	}
	.typeTitle{
		padding: 10px;
		overflow: auto;
		clear: both;
		background: #f2f2f2;
	}
	.typeTitle .li1{
		width: 412px;
		height: 30px;
		float: left;
		text-align: center;
		line-height: 30px;
	}
	.typeTitle .li2{
		width: 180px;
		height: 30px;
		float: left;
		text-align: center;
		line-height: 30px;
	}
	.typeTitle .li3{
		width: 180px;
		float: left;
		height: 30px;
		text-align: center;
		line-height: 30px;
	}
	.typeTitle .li4{
		width: 240px;
		float: left;
		height: 30px;
		text-align: center;
		line-height: 30px;
	}
</style>
